﻿using System;
using System.Collections;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Web;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using Microsoft.Win32;

namespace InfoJetSoft.Service.Worker
{
	
	//XSN文件(其实就是一个CAB文件)解压缩类.
	class XSNCABWorker
	{
		
		//解压缩Manifest的XSN,最后将所有CAB中的文件存储到manifest.CabFileList列表中.
		public static void  parseXSN(HttpContext context, InfoJetContext jetContext, Manifest manifest)
		{
			//调用extrac32.exe解压缩xsn文件.
			CABDecoder.doFDI( context, jetContext, manifest);
			//格式化XSN中的文件.主要是做编码转化或格式化一些特殊字符串.
			XSNCABFileFormatWorker.formatCABFile( manifest );
		}

		//在完成注册之后,释放一些不再使用的资源,减少内存占用.
		public static void  releaseXSN(Manifest manifest)
		{
			XSNConfig xsnConfig = manifest.XSNConfig;
			xsnConfig.Content = null;
			manifest.CabFileList = null;
			manifest.CabFile = null;

			//清空CABFile文件.
			ArrayList viewList = manifest.ViewList;
			if (viewList != null)
			{
				foreach (View view in viewList)
				{
					view.CabFile = null;
				}
			}

			//清空SchemaDef.
			ArrayList schemaDefList = manifest.SchemaDefList;
			if (schemaDefList != null)
			{
				foreach (SchemaDef schemaDef in schemaDefList)
				{
					schemaDef.CabFile = null;
				}
			}
			manifest.RootSchemaDef.CabFile = null;

			//删除SchemaLinke中的SchemaDoc.
			ArrayList schemaLinkList = manifest.SchemaLinkList;
			if (schemaLinkList != null)
			{
				foreach (SchemaLink schemaLink in schemaLinkList)
				{
					schemaLink.SchemaDoc = null;
				}
			}

            // Erucy: Remove
            //CID 0231 //2009.02.19 //Begin
            //删除临时目录.
            //if( ConfigUtil.DeleteTemplateFiles() )
            //{
            //    try
            //    {
            //        Directory.Delete( manifest.Folder, true );
            //    }
            //    catch( Exception e )
            //    {
            //    }
            //}
            //CID 0231 //2009.02.19 //End

			//CID 0467 //2009.11.20 //Begin
			foreach( DataObject dataObject in manifest.DataObjectList )
			{
				if( dataObject.WSAdapter != null && dataObject.WSAdapter.Input != null )
				{
					dataObject.WSAdapter.Input.SourceDoc = null;
				}
			}
			//CID 0467 //2009.11.20 //End
		}
		
	}
}	